package CustomUI;

import java.util.Timer;
import java.util.TimerTask;

import GameTools.ydPoint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Shader;
import android.net.MailTo;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class ydColorLineMajor extends View {

	ydPoint size;
	float r = 255;
	float b = 0;
	float g = 0;

	float dR;
	float dG;
	float dB;

	float pR;
	float pG;
	float pB;

	float delta = 0;

	int sections = 0;

	Paint paint = new Paint();
	Paint mPaint = new Paint();

	private float mCurrentHue = 50;
	private int mCurrentX = 0, mCurrentY = 0;
	public int mCurrentColor, mDefaultColor;
	private final int[] mHueBarColors = new int[258];
	private int[] mMainColors = new int[65536];
	ydOnTouchListeners listeners = new ydOnTouchListeners();
	public Canvas mCanvas;

	ydPoint picked = new ydPoint();
	
	Bitmap bitmap;
	Boolean bitUpdated=false;

	public ydColorLineMajor(Context context) {
		super(context);
		init();
	}

	public ydColorLineMajor(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		// TODO Auto-generated constructor stub
	}

	public ydColorLineMajor(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}

	private void updateMainColors() {
		int mainColor = getCurrentMainColor();
		int index = 0;
		int[] topColors = new int[256];
		for (int y = 0; y < 256; y++) {
			for (int x = 0; x < 256; x++) {
				if (y == 0) {
					mMainColors[index] = Color.rgb(
							255 - (255 - Color.red(mainColor)) * x / 255, 255
									- (255 - Color.green(mainColor)) * x / 255,
							255 - (255 - Color.blue(mainColor)) * x / 255);
					topColors[x] = mMainColors[index];
				} else
					mMainColors[index] = Color.rgb(
							(255 - y) * Color.red(topColors[x]) / 255,
							(255 - y) * Color.green(topColors[x]) / 255,
							(255 - y) * Color.blue(topColors[x]) / 255);
				index++;
			}
		}
	}

	// Get the current selected color from the hue bar
	public int getCurrentMainColor() {
		int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
		int index = 0;
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb(255, 0, (int) i);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb(255 - (int) i, 0, 255);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb(0, (int) i, 255);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb(0, 255, 255 - (int) i);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb((int) i, 255, 0);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) {
			if (index == translatedHue)
				return Color.rgb(255, 255 - (int) i, 0);
			index++;
		}
		return Color.RED;
	}

	public void init() {
		this.setDrawingCacheEnabled(true);
		size = new ydPoint(255, 255);
		// Get the current hue from the current color and update the main color
		// field
		float[] hsv = new float[3];
		int color = Color.GREEN;
		Color.colorToHSV(color, hsv);
		mCurrentHue = hsv[0];
		updateMainColors();

		this.setOnTouchListener(listeners);

		mCurrentColor = color;

		// Initialize the colors of the hue slider bar
		int index = 0;
		for (float i = 0; i < 256; i += 256 / 42) // Red (#f00) to pink (#f0f)
		{
			mHueBarColors[index] = Color.rgb(255, 0, (int) i);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) // Pink (#f0f) to blue (#00f)
		{
			mHueBarColors[index] = Color.rgb(255 - (int) i, 0, 255);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) // Blue (#00f) to light blue
													// (#0ff)
		{
			mHueBarColors[index] = Color.rgb(0, (int) i, 255);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) // Light blue (#0ff) to green
													// (#0f0)
		{
			mHueBarColors[index] = Color.rgb(0, 255, 255 - (int) i);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) // Green (#0f0) to yellow
													// (#ff0)
		{
			mHueBarColors[index] = Color.rgb((int) i, 255, 0);
			index++;
		}
		for (float i = 0; i < 256; i += 256 / 42) // Yellow (#ff0) to red (#f00)
		{
			mHueBarColors[index] = Color.rgb(255, 255 - (int) i, 0);
			index++;
		}
		setSize(new ydPoint(255, 255));
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.STROKE);

		listeners.addOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				float x = event.getX();
				float y = event.getY();

				mCurrentX = (int) x;
				mCurrentY = (int) y;
				
				if (x < 0) {
					x = 0;
				}
				if (x > size.x) {
					x = size.x;
				}
				if (y < 0) {
					y = 0;
				}
				if (y > size.y) {
					y = size.y;
				}
				
				int transX = mCurrentX;
				int transY = mCurrentY;
				int index = 256 * (transY - 1) + transX;
				if (index > 0 && index < mMainColors.length) {
					// Update the current color
					mCurrentColor = mMainColors[index];
					// Force the redraw of the dialog
					// invalidate();
				}
				return true;
			}
		});

		invalidate();

		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		mPaint.setTextAlign(Paint.Align.CENTER);

		mPaint.setTextSize(12);

		/*
		 * myTimer = new Timer(); myTimer.schedule(new TimerTask() {
		 * 
		 * @Override public void run() { postInvalidate(); }
		 * 
		 * }, 0, 100);
		 */

	}

	Timer myTimer;

	public void setSize(ydPoint size) {
		this.size = new ydPoint(size);

		delta = (255) / size.y;
		invalidate();
		return;
	}

	Shader shader;
	Path path = new Path();

	@Override
	public void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		setSize(new ydPoint(getWidth(), getHeight()));
		mCanvas = canvas;
		dR = (255 - r) / size.x;
		dG = (255 - g) / size.x;
		dB = (255 - b) / size.x;

		int index = 0;
		for (int x = 0; x < size.x; x++)

		{

			// Log.d("DRAWING GRAD", "TESTING");
			index = (int) ((x * delta));
			int[] colors = new int[2];

			colors[0] = mMainColors[index];

			colors[1] = Color.BLACK;

			shader = new LinearGradient(0, 0, 0, size.y, colors, null,
					Shader.TileMode.REPEAT);

			mPaint.setShader(shader);

			canvas.drawLine(x, 0, x, size.y, mPaint);

		}

		path.reset();
		path.moveTo(picked.x, picked.y);
		path.addCircle(picked.x, picked.y, 10, Direction.CCW);
		paint.setColor(Color.BLACK);
		canvas.drawPath(path, paint);

		path.reset();
		path.moveTo(0, 0);
		path.addRect(0, 0, size.x, size.y, Direction.CCW);

		paint.setColor(Color.WHITE);
		canvas.drawPath(path, paint);
		// canvas.drawText((getContext().getResources().getDisplayMetrics().heightPixels)
		// + "", 20, 20, paint);

		/*
		 * for(int i=0; i<size.x; i++){ for(int j=0; j<size.y; j++){
		 * 
		 * pR=r-j*delta-i*dR; pG=r-j*delta-i*dG; pB=r-j*delta-i*dB;
		 * 
		 * if(pR>255) pR=255; else if(pR<0) pR=0;
		 * 
		 * if(pG>255) pG=255; else if(pG<0) pG=0;
		 * 
		 * if(pB>255) pB=255; else if(pB<0) pB=0;
		 * 
		 * paint.setARGB(255, (int)pR, (int)pG, (int)pB); canvas.drawLine(i, j,
		 * i, j, paint);//(i, j, paint); } }
		 */
	}

	ydPoint lastTouch=new ydPoint();
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		mCurrentX = (int) x;
		mCurrentY = (int) y;
		int transX = mCurrentX;
		int transY = mCurrentY;
		int index = 256 * (transY - 1) + transX;
		if (index > 0 && index < mMainColors.length) {
			// Update the current color
			mCurrentColor = mMainColors[index];
			// Force the redraw of the dialog
			// invalidate();
		}
		lastTouch.setCoordinates(x,y);
		return true;
	}

	public void setColor(int r2, int g2, int b2) {
		r = r2;
		g = g2;
		b = b2;
		invalidate();

	}

	public void updateCurrentSelectedColor(float newHue) {
		mCurrentHue = newHue;

		updateMainColors();

		// Update the current selected color
		int transX = mCurrentX - 10;
		int transY = mCurrentY - 60;
		int index = 256 * (transY - 1) + transX;
		if (index > 0 && index < mMainColors.length)
			mCurrentColor = mMainColors[256 * (transY - 1) + transX];

		
		invalidate();
		bitUpdated=false;

	}

	public int getColor(int x, int y) {		
		if(bitUpdated==false){
			this.buildDrawingCache();
			bitmap=this.getDrawingCache(true);
			bitUpdated=true;
		}
		return bitmap.getPixel(x, y);
	}

	public int getSelectedColor() {
		if(bitUpdated==false){
			this.buildDrawingCache();
			bitmap=this.getDrawingCache(true);
			bitUpdated=true;
		}
		return bitmap.getPixel((int)lastTouch.x, (int)lastTouch.y);
	}

}
